Skip to content

Tbain/253 add tags count#506

Open
tbain wants to merge 7 commits intoopenedx:mainfrom
tbain:tbain/253_add_tags_count_rebased
Open

Tbain/253 add tags count#506
tbain wants to merge 7 commits intoopenedx:mainfrom
tbain:tbain/253_add_tags_count_rebased

Conversation

@tbain
Copy link
Copy Markdown

@tbain tbain commented Mar 18, 2026

Description

This implements openedx/modular-learning#253 , the task to add tag usage counts to the tags table under the taxonomies table. The frontend piece is where the results of this aggregation work is displayed is part of a separate pr to openedx/frontend-app-authoring. This change adds a subquery annotation onto the django query for retrieving tags. The original implementation of the counts for tags only counted raw usage of each tag, rather than aggregate sum of any tag and child tag usage with sibling de-duplication for the same usage (e.g. when two sibling nodes are used against the same course, module, etc. we still only need to count that as '1' for any parent/grandparent nodes) as specified in the AC for the issue above, so it was replaced with this more complicated sub-query that sums across tag usage based on various courses, sections, modules, and libraries that might use a tag.

Supporting information

Github issue with AC: openedx/modular-learning#253

Testing instructions

Refer to the AC in the Github Issue. Steps to verify this is implemented and working via UX (Note, depends on the frontend part of this ticket):

  1. Navigate to the "Studio home" page
  2. Navigate into an existing Course (or create a course and navigate into it)
  3. In the "Course Outline" page, add tag(s) from an existing taxonomy to the course, module, or section. Ensure at least one of the tags you add is a sub-tag of a root tag.
  4. Navigate back to the "Studio home" page
  5. Click the "Taxonomies" tab to navigate to the Taxonomies page
  6. Navigate into the Taxonomy that corresponds to the tag you added in step 3
  7. Observe that, if a tag is used, there is now an additional column on the table named "Usage Count" that is populated with bubbles that display the count of tags usages, if applicable
  8. Ensure that the tag you added in Step 3 properly associates the incremented count from its usage, and ensure that the usage count properly aggregates up the lineage based on the sub tag you selected in step 3

Other information

Include anything else that will help reviewers and consumers understand the change.

  • Does this change depend on other changes elsewhere?
    • this ticket is backwards compatible with the current implementation in frontend-app-authoring, since by default the frontend does not request the counts.
  • Any special concerns or limitations? For example: deprecations, migrations, security, or accessibility.
    • none at this time

@openedx-webhooks
Copy link
Copy Markdown

Thanks for the pull request, @tbain!

This repository is currently maintained by @axim-engineering.

Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review.

🔘 Get product approval

If you haven't already, check this list to see if your contribution needs to go through the product review process.

  • If it does, you'll need to submit a product proposal for your contribution, and have it reviewed by the Product Working Group.
    • This process (including the steps you'll need to take) is documented here.
  • If it doesn't, simply proceed with the next step.
🔘 Provide context

To help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:

  • Dependencies

    This PR must be merged before / after / at the same time as ...

  • Blockers

    This PR is waiting for OEP-1234 to be accepted.

  • Timeline information

    This PR must be merged by XX date because ...

  • Partner information

    This is for a course on edx.org.

  • Supporting documentation
  • Relevant Open edX discussion forum threads
🔘 Get a green build

If one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green.

Details
Where can I find more information?

If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources:

When can I expect my changes to be merged?

Our goal is to get community contributions seen and reviewed as efficiently as possible.

However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:

  • The size and impact of the changes that it introduces
  • The need for product review
  • Maintenance status of the parent repository

💡 As a result it may take up to several weeks or months to complete a review and merge your PR.

@openedx-webhooks openedx-webhooks added the open-source-contribution PR author is not from Axim or 2U label Mar 18, 2026
@github-project-automation github-project-automation bot moved this to Needs Triage in Contributions Mar 18, 2026
Copy link
Copy Markdown

@jesperhodge jesperhodge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seem to be changes missing. For example, src/taxonomy/data/api.ts.
Could you

  • review this PR and make sure that all necessary changes are in this branch? Compare to the open Unicon PR.
  • review discussions in the Unicon PR and either resolve them or copy them here to be addressed here.
  • fix any pipeline errors
    ?

@mgwozdz-unicon
Copy link
Copy Markdown
Contributor

Since we're no longer using recursive SQL for this, is it possible to update the PR description for accuracy?

@mphilbrick211 mphilbrick211 moved this from Needs Triage to In Eng Review in Contributions Mar 23, 2026
@tbain
Copy link
Copy Markdown
Author

tbain commented Mar 23, 2026

There seem to be changes missing. For example, src/taxonomy/data/api.ts. Could you

* review this PR and make sure that all necessary changes are in this branch? Compare to the open Unicon PR.

* review discussions in the Unicon PR and either resolve them or copy them here to be addressed here.

* fix any pipeline errors
  ?
  • src/taxonomy/data/api.ts, as an example, was a file in the front-end changes. I compared everything with the Backend changes/openedx-core and this is the correct set of files
  • All comments/issues to address from the aforementioned PR have been addressed with this one, so this PR is up to date
  • Working on that - I had missed a test suite that was affected by the changes so address that, still working on a strange quality issue where it's complaining about the time the unit test suite takes

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds rolled-up, de-duplicated tag usage counts (including ancestor rollups) to the tag listing query so the Taxonomies UI can display accurate “Usage Count” values per tag.

Changes:

  • Replaced the prior per-tag direct usage counting subquery with a dynamic, depth-aware subquery that rolls counts up to ancestors with per-object de-duplication.
  • Updated existing API/model tests to reflect rolled-up counts and added a broader set of usage-count test cases.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 9 comments.

File Description
src/openedx_tagging/models/base.py Centralizes and updates include_counts behavior by annotating tag querysets with rolled-up, de-duplicated usage_count via a subquery.
tests/openedx_tagging/test_models.py Updates expected usage counts and adds multiple new test scenarios validating ancestor rollup and sibling de-duplication.
tests/openedx_tagging/test_api.py Updates autocomplete/search test expectations to reflect rolled-up usage counts returned by the API when include_counts=True.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

def add_counts_query(self, qs: models.QuerySet):
"""
Adds a subquery to the passed-in queryset that returns the usage_count
for a given tag, or the appropriate count with de-deuplication per Object
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in docstring: “de-deuplication” should be “deduplication”.

Suggested change
for a given tag, or the appropriate count with de-deuplication per Object
for a given tag, or the appropriate count with deduplication per Object

Copilot uses AI. Check for mistakes.
Comment on lines +647 to +650
for a given tag, or the appropriate count with de-deuplication per Object
for the parents of a used child tag
:param qs: The QuerySet to annotate with usage counts.
:return: the queryset annotated with the usage counts
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This docstring uses Sphinx-style ":param"/":return" fields, but other docstrings in this module don’t. For consistency (and to avoid mixed docstring formats), please rewrite this docstring to match the prevailing style used elsewhere in this file.

Suggested change
for a given tag, or the appropriate count with de-deuplication per Object
for the parents of a used child tag
:param qs: The QuerySet to annotate with usage counts.
:return: the queryset annotated with the usage counts
for a given tag, or the appropriate count with de-duplication per object
for the parents of a used child tag.
The ``qs`` argument is the QuerySet to annotate with usage counts, and
the returned queryset is annotated with those usage counts.

Copilot uses AI. Check for mistakes.
# build a list of lineage paths to be used in the query, so we're not hard coding to
# a certain number of levels. This will build an array containing something like:
# ['tag_id', 'tag__parent_id', 'tag__parent__parent_id', 'tag__parent__parent__parent_id', ...]
lineage_paths = [f"tag{'__parent' * i}_id" for i in range(TAXONOMY_MAX_DEPTH+1)]
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP8/style consistency: add spaces around the "+" in range(TAXONOMY_MAX_DEPTH+1) (elsewhere in this file it’s written as TAXONOMY_MAX_DEPTH + 1).

Suggested change
lineage_paths = [f"tag{'__parent' * i}_id" for i in range(TAXONOMY_MAX_DEPTH+1)]
lineage_paths = [f"tag{'__parent' * i}_id" for i in range(TAXONOMY_MAX_DEPTH + 1)]

Copilot uses AI. Check for mistakes.
Comment on lines +686 to +687
# change in TAXONOMY_MAX_DEPTH, now it is dynamic to reduce maintenace
# (Thanks Google for helping me build this)
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parenthetical “Thanks Google for helping me build this” doesn’t add technical context and is likely to become noise in long-lived code. Please remove it or replace it with a brief explanation of the actual rationale/constraints (e.g., why the Q needs to be built dynamically).

Suggested change
# change in TAXONOMY_MAX_DEPTH, now it is dynamic to reduce maintenace
# (Thanks Google for helping me build this)
# change in TAXONOMY_MAX_DEPTH; now it is built dynamically to reduce
# maintenance overhead and keep the query aligned with the configured depth.

Copilot uses AI. Check for mistakes.
def test_usage_count_multiple_objects_same_tag_deep(self) -> None:
"""
AI/Claude4.6 generated via IntelliJ IDEA AI Assistant
When two distinct objects (e.g. seperate courses, modules, etc.) are tagged
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in docstring: “seperate” should be “separate”.

Suggested change
When two distinct objects (e.g. seperate courses, modules, etc.) are tagged
When two distinct objects (e.g. separate courses, modules, etc.) are tagged

Copilot uses AI. Check for mistakes.
# ...
# ```
# Previously the above was hard coded and needed to be changed with every
# change in TAXONOMY_MAX_DEPTH, now it is dynamic to reduce maintenace
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in comment: “maintenace” should be “maintenance”.

Suggested change
# change in TAXONOMY_MAX_DEPTH, now it is dynamic to reduce maintenace
# change in TAXONOMY_MAX_DEPTH, now it is dynamic to reduce maintenance

Copilot uses AI. Check for mistakes.
Comment on lines +640 to +644
qs = self.add_counts_query(qs)

return qs # type: ignore[return-value]

def add_counts_query(self, qs: models.QuerySet):
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add_counts_query is only used internally by get_filtered_tags paths. Making it a public Taxonomy method expands the model’s API surface unnecessarily; consider renaming to _add_counts_query (or similar) and adding an explicit return type (e.g., TagDataQuerySet/models.QuerySet) to clarify intended usage.

Suggested change
qs = self.add_counts_query(qs)
return qs # type: ignore[return-value]
def add_counts_query(self, qs: models.QuerySet):
qs = self._add_counts_query(qs)
return qs # type: ignore[return-value]
def _add_counts_query(self, qs: TagDataQuerySet) -> TagDataQuerySet:

Copilot uses AI. Check for mistakes.
"""
Test that the usage count in the results is right
Test that the usage count in the results is right for a basic case;
many objects tagged seperately should return a simple usage count that
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in test docstring: “seperately” should be “separately”.

Suggested change
many objects tagged seperately should return a simple usage count that
many objects tagged separately should return a simple usage count that

Copilot uses AI. Check for mistakes.
Comment on lines +598 to +603
"""
AI/Claude4.6 generated via IntelliJ IDEA AI Assistant
When a child tag (depth 3) is applied to an object, it should
roll up the count to all its ancestors when using _get_filtered_tags_deep.
The child tag and each of its ancestors should have usage_count=1.
"""
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Several new test docstrings include tool-attribution text (e.g., “AI/Claude4.6 generated via IntelliJ IDEA AI Assistant”). This doesn’t document test behavior and is inconsistent with typical test docstrings; please remove the attribution lines and keep the docstrings focused on the scenario/assertions.

Copilot uses AI. Check for mistakes.
# build a list of lineage paths to be used in the query, so we're not hard coding to
# a certain number of levels. This will build an array containing something like:
# ['tag_id', 'tag__parent_id', 'tag__parent__parent_id', 'tag__parent__parent__parent_id', ...]
lineage_paths = [f"tag{'__parent' * i}_id" for i in range(TAXONOMY_MAX_DEPTH+1)]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using TAXONOMY_MAX_DEPTH for this query, what about using the actual max depth of the current taxonomy? e.g. max_depth = qs.aggregate(models.Max("depth", default=0))["depth__max"] ?

@bradenmacdonald
Copy link
Copy Markdown
Contributor

Feel free to ping me for review here once the AC are clarified and the comments from Copilot etc are addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

open-source-contribution PR author is not from Axim or 2U

Projects

Status: In Eng Review

Development

Successfully merging this pull request may close these issues.

7 participants